sink("segregation.channel-psrm-log.txt",append=F,type="output")

library(plyr)
library(SDMTools)
library(xtable)

######
###Aggregate Traffic Tables/Figures
######
load('url.survey.wlabels.RData')

#High Web Traffic Table
url.survey.wlabels$rep.pool <- ifelse(url.survey.wlabels$lean.rep==1|url.survey.wlabels$not.strong.rep==1|url.survey.wlabels$strong.rep==1, 1, 0)
url.survey.wlabels$dem.pool <- ifelse(url.survey.wlabels$lean.dem==1|url.survey.wlabels$not.strong.dem==1|url.survey.wlabels$strong.dem==1, 1, 0)
site.ratings.party.3pt <- ddply(url.survey.wlabels,.(domain),summarise,dem=wt.mean(x=dem.pool,wt=weight_pulse),independent=wt.mean(x=independent,wt=weight_pulse),rep=wt.mean(x=rep.pool,wt=weight_pulse),site.visits=length(caseid))
allsites.3pt <- site.ratings.party.3pt[order( site.ratings.party.3pt$rep,decreasing=TRUE),]
allsites.3pt <- subset(allsites.3pt, allsites.3pt$site.visits > 30000)

#Table 1 Main Text Results
xtable(allsites.3pt[1:10,],caption="Top Ten News Domains")

#Figures of Web Traffic Share Within Each Party for Domestic News Websites
dem.visits <- subset(url.survey.wlabels,url.survey.wlabels$rep.partisanship==0 & !is.na(url.survey.wlabels$type))
dem.visits.share <- ddply(dem.visits,.(domain),summarise,total=length(domain))
dem.visits.total <- sum(dem.visits.share$total)
dem.visits.all.site <- dem.visits.share
dem.visits.share <- dem.visits.share[order(dem.visits.share$total,decreasing=TRUE),][1:20,]
dem.visits.share$share <- dem.visits.share$total/dem.visits.total
dem.visits.share <- dem.visits.share[order(dem.visits.share$total),]

rep.visits <- subset(url.survey.wlabels,url.survey.wlabels$rep.partisanship==1 & !is.na(url.survey.wlabels$type))
rep.visits.share <- ddply(rep.visits,.(domain),summarise,total=length(domain))
rep.visits.total <- sum(rep.visits.share$total)
rep.visits.all.site <- rep.visits.share
rep.visits.share <- rep.visits.share[order(rep.visits.share$total,decreasing=TRUE),][1:20,]
rep.visits.share$share <- rep.visits.share$total/rep.visits.total
rep.visits.share <- rep.visits.share[order(rep.visits.share$total),]

pdf(file='figure-1.pdf',height=6,width=12)
par(mar=c(4,10.5,2,1),mfrow=c(1,2))
barplot(height=dem.visits.share$share,horiz=TRUE,xlim=c(0,.2),names.arg=dem.visits.share$domain,las=1,xlab='Traffic Share',main='Democrats',cex.names=1.1,cex.lab=1.2)
barplot(height=rep.visits.share$share,horiz=TRUE,xlim=c(0,.2),names.arg=rep.visits.share$domain,las=1,xlab='Traffic Share',main='Republicans',cex.names=1.1,cex.lab=1.2)
dev.off()

######
###Distribution of Slant and Topic Coverage
######
table(url.survey.wlabels$rating.scale)/sum(table(url.survey.wlabels$rating.scale))
table(url.survey.wlabels$high.level)/sum(table(url.survey.wlabels$high.level))

######
###Segregation and Channel Analysis
######

site.ratings.party <- ddply(url.survey.wlabels,.(domain),summarise,rep.percent.all=weighted.mean(x=rep.partisanship.all,wt=weight_pulse,na.rm=TRUE))
url.survey.wlabels <- merge(url.survey.wlabels, site.ratings.party,by=c('domain'))

individual.use.channel <- ddply(url.survey.wlabels,.(caseid,channel,weight_pulse),summarise,rep.all=mean(rep.percent.all,na.rm=TRUE),total.visits=length(caseid))
individual.use.channel <- individual.use.channel[which(!is.na(individual.use.channel$weight_pulse)),]

#Overall segregation to compare with Flaxman et al. 2016 (Reported in main Text)
sqrt(2)*wt.sd(individual.use.channel$rep.all,individual.use.channel$weight_pulse)

#Segregation by channel to compare with Flaxman et al. 2016
channel.summary <- ddply(individual.use.channel,.(channel),summarise,all.segregation=sqrt(2)*wt.sd(rep.all,weight_pulse),total.visits=sum(total.visits*weight_pulse,na.rm=TRUE))
channel.summary$total.visit.share <- channel.summary$total.visits/sum(channel.summary$total.visits)

#Table 3 Main Text Results
channel.summary

sink()